﻿Imports SUPER3D1.MODViewer
Public Class MaterialEditor
    Dim curMat As Material = Nothing
    Dim newM As Boolean
    Dim opening As Boolean = True
    Dim cur As Integer = 0
    Dim mID As String = ""
    Sub New(Optional ByVal fn As String = "", Optional ByVal ind As Integer = -1)
        cur = ind
        InitializeComponent()
        popSelection()
        populate(ind)
        opening = False
        Me.Show()
    End Sub
    Sub populate(ByVal ind As Integer)
        If ind = -1 Then ind = 0
        cur = 0
        With loadedModels(0).ModelData.Materials(ind)
            If Not IsNothing(.mtlId) Then
                mID = .mtlId
                curMat = loadedModels(0).ModelData.Materials(ind)
                Me.Text = "Editing material " & .mtlId
                TextBox1.Text = .mtlId
                TrackBar1.Value = CInt(.specular * 255)
                TrackBar2.Value = CInt(.optdensity * 255)
                TrackBar3.Value = CInt(.dissolve * 255)
                Label1.Text = System.IO.Path.GetFileName(.texFile)
                Select Case .texCol
                    Case Cols.AMBIENT
                        CheckBox1.Checked = True
                    Case Cols.DIFFUSE
                        CheckBox3.Checked = True
                    Case Cols.SPECULAR
                        CheckBox2.Checked = True
                End Select
            Else
                Me.Text = "Creating new material"
                TextBox1.Text = "New Material"
                With curMat
                    .ambientCol = New Double() {1, 1, 1, 1}
                    .diffuseCol = New Double() {1, 1, 1, 1}
                    .emissiveCol = New Double() {1, 1, 1, 1}
                End With
            End If
        End With
        SetColors()
    End Sub

    Sub SetColors()
        With curMat
            Button1.BackColor = _
              Color.FromArgb(.ambientCol(1) * 255, .ambientCol(2) * 255, .ambientCol(3) * 255)

            Button2.BackColor = _
                Color.FromArgb(.diffuseCol(1) * 255, .diffuseCol(2) * 255, .diffuseCol(3) * 255)

            Button3.BackColor = _
                Color.FromArgb(.emissiveCol(1) * 255, .emissiveCol(2) * 255, .emissiveCol(3) * 255)
        End With
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        colDialog.ShowDialog()
        curMat.ambientCol = fromColorToARGB(colDialog.Color)
        Button1.BackColor = colDialog.Color
        SetColors()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs)
        loadedModels(0).ModelData.Materials(cur) = curMat
    End Sub
    Sub SaveChanges()
        Dim ind As Integer = locatemat(loadedModels(0).ModelData.Materials, mID)
        curMat.mtlId = TextBox1.Text
        If IO.File.Exists(Label1.Text) Then curMat.texFile = workingDir & Label1.Text
        mID = curMat.mtlId
        loadedModels(0).ModelData.Materials(ind) = curMat
    End Sub
    Sub popSelection(Optional ByVal old As Integer = -1)
        opening = True
        Dim oldSel As Integer = ComboBox1.SelectedIndex
        ComboBox1.Items.Clear()
        For i As Integer = 0 To loadedModels(0).ModelData.Materials.Length - 1
            ComboBox1.Items.Add(loadedModels(0).ModelData.Materials(i).mtlId)
        Next
        If old > -1 Then ComboBox1.SelectedIndex = old Else ComboBox1.SelectedIndex = 0
        opening = False
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim opn As OpenFileDialog = New OpenFileDialog
        With opn
            .FileName = ""
            .Filter = "Image files (*.jpg, *.png)|*.jpg;*.png"
            .InitialDirectory = workingDir
        End With
        Dim f As String = IO.Path.GetFileName(opn.FileName)
        If opn.ShowDialog = Windows.Forms.DialogResult.OK Then
            If IO.File.Exists(workingDir & f) Then Label1.Text = f
            curMat.texFile = f
        End If
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        SaveChanges()
        loadedModels(0).WriteMtl(loadedModels(0).ModelData.Materials)
        popSelection(loadedModels(0).ModelData.Materials.Length - 1)
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        curMat.mtlId = TextBox1.Text
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Not opening Then populate(ComboBox1.SelectedIndex)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        colDialog.ShowDialog()
        curMat.diffuseCol = fromColorToARGB(colDialog.Color)
        Button2.BackColor = colDialog.Color
        SetColors()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        colDialog.ShowDialog()
        curMat.emissiveCol = fromColorToARGB(colDialog.Color)
        Button1.BackColor = colDialog.Color
        SetColors()
    End Sub
    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        curMat.specular = CDbl(TrackBar1.Value / 255.0F)
        Label5.Text = CSng(curMat.optdensity)
    End Sub

    Private Sub TrackBar2_Scroll(sender As Object, e As EventArgs) Handles TrackBar2.Scroll
        curMat.optdensity = CDbl(TrackBar2.Value / 255.0F)
        Label7.Text = CSng(curMat.optdensity)
    End Sub

    Private Sub TrackBar3_Scroll(sender As Object, e As EventArgs) Handles TrackBar3.Scroll
        curMat.dissolve = CDbl(TrackBar3.Value / 255.0F)
        Label8.Text = CSng(curMat.dissolve)
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        curMat.texCol = Cols.AMBIENT
        CheckBox2.Checked = False
        CheckBox3.Checked = False
    End Sub

    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        curMat.texCol = Cols.SPECULAR
        CheckBox1.Checked = False
        CheckBox3.Checked = False
    End Sub

    Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
        curMat.texCol = Cols.DIFFUSE
        CheckBox1.Checked = False
        CheckBox3.Checked = False
    End Sub

    Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
        ReDim Preserve loadedModels(0).ModelData.Materials(loadedModels(0).ModelData.Materials.Length)
        loadedModels(0).ModelData.Materials(loadedModels(0).ModelData.Materials.Length - 1) = curMat
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        SaveChanges()
    End Sub
End Class